package com.myProject.control.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TDisease;
import com.myProject.bean.TUser;
import com.myProject.serves.Impl.UserSevImpl;
import com.myProject.serves.UserSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserServlet", urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    UserSev us = new UserSevImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || med.equalsIgnoreCase("")) {
            showAll(request, response);
        } else if (med.equalsIgnoreCase("add")) {
            addUser(request, response);
        } else if (med.equalsIgnoreCase("delete")) {
            deleteUser(request, response);
        } else if (med.equalsIgnoreCase("alt")) {
            updateUser(request, response);
        }
    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String level = request.getParameter("level");
        String realname = request.getParameter("realname");
        String pkid = request.getParameter("pkId");
        TUser tUser = new TUser();
        tUser.setUser(userName);
        tUser.setPassword(password);
        tUser.setLevel(Integer.parseInt(level));
        tUser.setRealname(realname);
        tUser.setPkId(Integer.parseInt(pkid));
        us.updateUser(tUser);
    }

    private void deleteUser(HttpServletRequest request, HttpServletResponse response) {
        int x = Integer.parseInt(request.getParameter("pkId"));
        TUser tUser = new TUser();
        tUser.setPkId(x);
        us.deleteUser(tUser);
    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String level = request.getParameter("level");
        String realname = request.getParameter("realname");
        TUser tUser = new TUser();
        tUser.setUser(userName);
        tUser.setPassword(password);
        tUser.setLevel(Integer.parseInt(level));
        tUser.setRealname(realname);
        us.addUser(tUser);
    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);
        int x = 10000;
        String type = request.getParameter("userType");
        if (type != null) {
            x = Integer.parseInt(type);
        }
        TUser tUser = new TUser();
        tUser.setLevel(x);
        PageInfo<TUser> show = us.showUserByPage(pageNo, pageSize, tUser);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);
    }
}
